<?php
use Util\Validate;

class SettingController extends BaseController
{
    CONST STAFF_ONE = 1;//客户
    CONST STAFF_TWO = 2;//员工（用户）
    private $req;

    public function init()
    {
        $this->req = $this->getRequest();
        // 全局搜索专用
        $this->view->breadcrumb = array(array(
            'url'=>'',
            'name' => '设置',
        ));
    }

    /**
     * 资金管理
     */
    public function indexAction()
    {
        $this->redirect('/capital/collection-account');
    }

    /**
     * @return array
     * 员工列表
     */
    public function staffListAction()
    {
        $staffBiz = new \Biz\Staff();
        // 检查请求
        if (!$this->req->isGet()) {
            return Response::Error(0, '请求方式错误');
        }
        $searchArr = array();
        // 获取每页显示条数
        $size = Page::ValidatePageSize((int)$this->req->get('pageSize'), '/setting/staff-list');
        // 获取当前页数
        $now = $this->req->get('p') ? $this->req->get('p') : 1;
        $search_text= $this->req->get('searchText');
        $is_use = $this->req->get('is_use');

        $offset = ($now - 1) * $size;
        $limit = $size;
        $where= "";
        if($is_use != ''){
            $searchArr['is_use'] = $is_use;
            $where .= " `is_use` = ".$is_use;
            if($search_text != ''){
                $searchArr['searchText'] = $search_text;
                $where .= " AND `account` like '%".$search_text."' OR `name` like '%".$search_text."' OR `tel` like '%".$search_text."' ";
            }
        }else{
            if($search_text != ''){
                $searchArr['searchText'] = $search_text;
                $where .= " `account` like '%".$search_text."' OR `name` like '%".$search_text."' OR `tel` like '%".$search_text."' ";
            }
        }
        $order_by = " `id` desc";
        $staff_list = $staffBiz->getStaffList($where,$offset,$limit,$order_by);
        $total = !empty($staff_list) ? $staff_list['total'] : 0;
        $total_page = ceil($total / $size);
        $page = new Page($total, $size, $now, '/setting/staff-list?'.$this->makeUrl($searchArr), 2);
        $this->view->list = !empty($staff_list) ? $staff_list['lists'] : array();
        $this->view->size = $size;
        $this->view->now = $now;
        $this->view->page = $page->showPage();
        $this->view->total_page = $total_page;
        $this->view->search_text = $search_text;
        $this->view->is_use = $is_use;
        $this->view->breadcrumb[] = array(
            'name' => '员工列表',
        );
    }

    /**
     * 新增员工
     */
    public function staffCreateAction()
    {
        $login = new Zend_Session_Namespace('login');
        $admin_id = $login->user_id;//登录员工账号ID
        if($this->req->isPost()){
            $staffInfo = array();
            $staffInfo['account'] = $this->req->getPost('account');
            $staffInfo['name'] = $this->req->getPost('name');
            $staffInfo['enterprise_role_id'] = $this->req->getPost('role');
            $staffInfo['tel'] = $this->req->getPost('tel');
            $staffInfo['email'] = $this->req->getPost('email');
            $staffInfo['remark'] = $this->req->getPost('remark');
            $staffInfo['admin_id'] = $this->req->getPost('admin_id');
            $staffInfo['pwd'] = md5($this->req->getPost('password'));

            if ($staffInfo['account'] == '') {
                return Response::Error(0, ' 登录账号不能为空！');
            }
            if ($staffInfo['pwd'] == '') {
                return Response::Error(0, ' 登录密码不能为空！');
            }
            if ($staffInfo['name'] == '') {
                return Response::Error(0, ' 员工姓名不能为空！');
            }
            if ($staffInfo['tel'] != '') {
                if (!Validate::test('mobile', $staffInfo['tel'])) {
                    return Response::Error(0, '手机号码格式不正确');
                }
            }
            if ($staffInfo['email'] != '') {
                if (!Validate::test('email', $staffInfo['email'])) {
                    return Response::Error(0, '邮箱格式不正确');
                }
            }
            //校验登录账户唯一性
            $staffBiz = new \Biz\Staff();
            if ($staffBiz::checkAccountUsability($staffInfo['account'],self::STAFF_TWO) !== true) {
                return Response::Error(0, '登录账号不能重复');
            }
            $staffInfo['type'] = self::STAFF_TWO;
            //插入新建员工
            if($staffBiz->insert($staffInfo)){
                return Response::Success('', '创建成功',200);
            }else{
                return Response::Error(0, '创建失败');
            }
        }
        $this->view->admin_id = $admin_id;
        $this->view->breadcrumb[] = array(
            'name' => '新增员工',
        );
    }

    /**
     * 编辑员工
     */
    public function staffEditAction(){
        // 检查请求
        if (!$this->req->isGet()) {
            return Response::Error(0, '请求方式错误');
        }
        $id = $this->req->get('id');
        if($id == ''){
            return Response::Error(0, 'ID不能为空');
        }
        $type = $this->req->get('type');
        $staffBiz = new \Biz\Staff();
        $staffInfo = $staffBiz->getById($id);
        if(empty($staffInfo)){
            return Response::Error(0, '员工信息不存在');
        }
        if($type == 'edit'){
            $is_use = $this->req->get('is_use');
            if($is_use == ''){
                $staffInfo['account'] = $this->req->get('account');
                $staffInfo['name'] = $this->req->get('name');
                $staffInfo['enterprise_role_id'] = $this->req->get('role');
                $staffInfo['tel'] = $this->req->get('tel');
                $staffInfo['email'] = $this->req->get('email');
                $staffInfo['remark'] = $this->req->get('remark');
                if ($staffInfo['account'] == '') {
                    return Response::Error(0, ' 登录账号不能为空！');
                }
                if ($staffInfo['name'] == '') {
                    return Response::Error(0, ' 员工姓名不能为空！');
                }
                if ($staffInfo['tel'] != '') {
                    if (!Validate::test('mobile', $staffInfo['tel'])) {
                        return Response::Error(0, '手机号码格式不正确');
                    }
                }
                if ($staffInfo['email'] != '') {
                    if (!Validate::test('email', $staffInfo['email'])) {
                        return Response::Error(0, '邮箱格式不正确');
                    }
                }
                //编辑员工
                if($staffBiz->update($staffInfo)){
                    return Response::Success('', '保存成功',200);
                }else{
                    return Response::Error(0, ' 保存失败');
                }
            }else{
                $staffInfo['is_use'] = $is_use;
                if($staffBiz->update($staffInfo)){
                    return Response::Success('', '保存成功',200);
                }else{
                    return Response::Error(0, ' 保存失败');
                }
            }
        }
        $staffBiz = new \Biz\Staff();
        $staffInfo = $staffBiz->getById($id);
        $this->view->detail = $staffInfo;
        $this->view->breadcrumb[] = array(
            'name' => '编辑员工',
        );
    }


    /**
     * 角色权限
     */
    public function privilegeAction()
    {
        $this->view->title = '角色权限';
    }

    /**
     * 角色权限
     */
    public function privilegeCreateAction()
    {
        $this->view->title = '新增角色';
    }

}